package com.team.project.controller;

import com.team.project.service.intf.AdminService;
import com.team.project.util.VerifyCodeUtil;
import com.team.project.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author 逗逗
 * 2022/11/22 17:29
 */
@RestController
@RequestMapping("/api/admin/")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("login.do")
    public R login(String name, String password, String code, HttpSession session) {
        return adminService.login(name,password,code,session);
    }

    @RequestMapping("createcode.do")
    public void createImg(HttpServletResponse response, HttpSession session) throws IOException {
        //1.生成对应宽高的初始图片
        BufferedImage verifyImg=new BufferedImage(200,50,BufferedImage.TYPE_INT_RGB);
        //2.单独的一个类方法，出于代码复用考虑，进行了封装。功能是生成验证码字符并加上噪点，干扰线，返回值为验证码字符
        String randomText = VerifyCodeUtil.drawRandomText(200,50,verifyImg);
        //3.服务端记录验证码
        session.setAttribute("code", randomText);
        System.err.println(randomText);
        //4.必须设置响应内容类型为图片，否则前台不识别
        response.setContentType("image/png");
        //5.写出图片
        ImageIO.write(verifyImg,"png",response.getOutputStream());
    }
}
